import api from '@/api/exam'
import { deepCopy, setPage } from '@/utils/util'

const initPage = {
    size: 10,
    number: 0,
    total: 0,
    pages: 1,
    filter: {
        enabled: null,
        classIds: null,
        name: null
    }
}
const state = {
    data: {
        examList: [],
        evidences: [],
        examStoreData: [],
        examDetailData: [],
        examMinutes: null,
        examStartTime: null
    },
    ui: {
        page: deepCopy(initPage)
    },
    examdata: {
        page: deepCopy(initPage)
    }
}

// getters
const getters = {
    examList: state => state.data.examList,
    examStoreData: state => state.data.examStoreData,
    examDetailData: state => state.data.examDetailData,
    examMinutes: state => state.data.examMinutes,
    examStartTime: state => state.data.examStartTime
}

// actions
// 分页查询培训任务
const actions = {
    getexamlist({ commit }, data = {}) {
        api.getstudyExam(JSON.stringify(data) != '{}' ? data : state.ui.page, {
            loading: data.loading === false ? false : true,
            success: res => {
                const { pages, total } = res
                commit('SETEXAMPAGE', { pages, total })
                commit('setsexamList', res)
            }
        })
    },
    // 删除某一条数据
    removeone_exam({ dispatch }, data) {
        api.removeoneExam(data, {
            success: () => {
                dispatch('getexamlist')
            }
        })
    },
    // 添加任务
    addExam({ dispatch, commit }, data) {
        api.createExam(data.data, {
            success: exam => {
                commit('setCurrentExamInfo', exam)
                dispatch('getexamlist')
                data.cb && data.cb(exam)
            }
        })
    },
    // 根据ID获取任务列表
    queryFindExam({ commit }, id) {
        api.queryOneExam(id, {
            success: exam => {
                commit('setsexamList', exam)
            }
        })
    },
    // 根据ID更新任务
    updateExam({ dispatch, commit }, data) {
        api.updateExam(data, {
            success: exam => {
                commit('setCurrentExamInfo', exam)
                dispatch('getexamlist')
            }
        })
    },
    // 根据用户工号查询用户信息
    getexamstore({ commit }, data = {}) {
        api.examStore(JSON.stringify(data) != '{}' ? data : state.ui.page, {
            loading: data.loading === false ? false : true,
            success: res => {
                const { pages, total } = res
                commit('SETEXAMPAGE', { pages, total })
                commit('getExamList', res)
            }
        })
    },
    getexamdetail({ commit }, data) {
        state.examdata.page.filter.businessKeyword = ''
        state.examdata.page.filter.taskId = data
        api.examDetail(state.examdata.page, {
            loading: false,
            success: res => {
                const { pages, total } = res
                commit('getExamDetail', res)
            }
        })
    },
    getExamById({ commit }, { examId }) {
        examId &&
            api.queryOneExam(examId, {
                success: exam => {
                    commit('setCurrentExamInfo', exam)
                }
            })
    }
}

// mutations
const mutations = {
    setsexamList(state, payload) {
        state.data.examList = payload
    },
    getExamList(state, payload) {
        state.data.examStoreData = payload
    },
    getExamDetail(state, payload) {
        state.data.examDetailData = payload
    },
    SETEXAMPAGE(state, page) {
        page && setPage(state.ui.page, page)
    },
    SETEXAMPAGESTORE(state, page) {
        page && setPage(state.examdata.page, page)
    },
    setEvidences(state, payload) {
        state.data.evidences = payload.content
    },
    setCurrentExamInfo(state, payload) {
        console.log('setCurrentExamInfo', payload.startTime, payload.examMinutes)
        state.data.examStartTime = payload.startTime
        state.data.examMinutes = payload.examMinutes
    }
}

export default {
    state,
    getters,
    actions,
    mutations
}
